package com.cai.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.cai.dao.PermissionDao;
import com.cai.entity.PageResult;
import com.cai.entity.QueryPageBean;

import com.cai.pojo.Permission;
import com.cai.service.PermissionService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    //分页查询
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //使用分页助手
        PageHelper.startPage(currentPage, pageSize);
        Page<Permission> page = permissionDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Permission> rows = page.getResult();
        return new PageResult(total, rows);

    }
    //删除权限
    public void deleteById(Integer id) {
        //先判断是否关联角色,如果关联角色需要先删除关联
        long conut = permissionDao.findPermissionAndRole(id);
        if(conut>0){
            permissionDao.deleteAssociation(id);
        }
        permissionDao.deleteById(id);
    }
    //添加权限
    public void add(Permission permission) {
        permissionDao.add(permission);
    }
    //根据id查询权限
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }
    //编辑权限
    public void edit(Permission permission) {
    permissionDao.edit(permission);
    }

    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }
}
